﻿$(function(){
	var timestamp;
	var nonceStr;
	var signature;
	var url = 'http://yx.fxtourong.com/V1/Goods/create_zj.html';
	var imageId1;
	
	var imageServerId1;

	var is_encrypt = "0";
	var pwd = "";
	
	var is_pay = "0";
	var price;
	
	var type_id = "情感";
	
	$.ajax({
			type: "POST",
			url: "http://yx.fxtourong.com/V1/Wechat/getSignPackage",
			data: {
				token : localStorage.getItem("token"),
				url : url,
				verify : "yin@xiong8_(fx-tourong"
			},
			dataType: "json",
			success: function(data) {
				console.log(data);
				var code = data.code;
				if(code == "1") {
					timestamp = data.data.timestamp;
					nonceStr = data.data.nonceStr;
					signature = data.data.signature;
					wx.config({
					    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					    appId: 'wx7f13802caf31f385', // 必填，公众号的唯一标识
					    timestamp: timestamp, // 必填，生成签名的时间戳
					    nonceStr: nonceStr, // 必填，生成签名的随机串
					    signature: signature,// 必填，签名，见附录1
					    jsApiList: ['chooseImage','startRecord','stopRecord',
							'onVoiceRecordEnd','playVoice','pauseVoice',
							'stopVoice','onVoicePlayEnd','uploadVoice',
							'downloadVoice','uploadImage','downloadImage'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
					});
					wx.ready(function(){
						console.log("aaaaaaaaaaaaaaaa");
						$("#image1").click(function(){
							wx.chooseImage({
		    						count: 1, // 默认9
		    						sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
		    						sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
		    						success: function (res) {
		    							onImageUp(res.localIds.toString(), 1);
		        						imageId1 = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
										$("#image1").attr('src',imageId1); 
		    						}
							});
							
						});
					});
				}
			}
		});
		
	function onImageUp(path, index) {
		wx.uploadImage({
			localId: path, // 需要上传的图片的本地ID，由chooseImage接口获得
			isShowProgressTips: 0, // 默认为1，显示进度提示
			success: function(res) {
				if(index == 1){
					 imageServerId1 = res.serverId; // 返回图片的服务器端ID
				} 
			}
		});
	}
		
	$("select#sel_pwd").change(function(){
		var sel_yn = $(this).val();
		if(sel_yn == "是"){
			$("#create_pwd").css('display','block'); 
			is_encrypt = "1";
		} else {
			$("#create_pwd").css('display','none'); 
			is_encrypt = "0";
		}
	});
	
	$("select#sel_pay").change(function(){
		var sel_yn = $(this).val();
		if(sel_yn == "是"){
			$("#create_pay").css('display','block'); 
			$("#create_free").css('display','none'); 
			is_pay = "1";
		} else {
			$("#create_pay").css('display','none'); 
			$("#create_free").css('display','block'); 
			is_pay = "0";
		}
	});
	
	$("select#sel_type").change(function(){
		type_id = $(this).val();
	});
	
	$("#post_create_topic").click(function(){
		var topname = $("#topic_name").val();
		var topdetail = $("#topic_detail").val();
		var imageServerIds = imageServerId1;
		if(imageServerIds == null || imageServerIds == undefined || imageServerIds == ''){
			$("#alert_ts").text("请上传专辑封面");
			$("#post_create_topic").attr("data-am-modal", "{target: '#my-alert'}");
			return;
		} else {
			$("#post_create_topic").removeAttr("data-am-modal");
		}
		if(is_encrypt == "0"){
			pwd = "";
		} else {
			pwd = $("#create_pwd").val();
		}
		if(is_pay == "0"){
			price = "";
		} else {
			price = $("#create_price").val();
		}
		$("#data_json").text("1");
		$("#audio").text("3:" + recordServerPath1);
		$("#picture").text("参数:" + topname + ":" + imageServerIds + ":" + topdetail +  ":" + is_encrypt + ":" + pwd + ":" + is_pay + ":" 
		+ price + ":" + type_id + ":" + timeStr);
		
		var store_id = localStorage.getItem("store_id");
		var urlPath = "http://yx.fxtourong.com/V1/Goods/createAlbum" + "?store_id=" + store_id;
			$.ajax({
				type: "POST",
				url: urlPath,
				data: {
					token : localStorage.getItem("token"),
					name : topname,
					cover : imageServerIds,
					details : topdetail,
					is_encrypt : is_encrypt,
					pwd : pwd,
					is_pay : is_pay,
					price : price,
					type_id : type_id
				},
				dataType: "json",
				success: function(data) {
					$("#data_json").text("2");
					console.log(data);
					var code = data.code;
					console.log(code);
					$("#data_json").text("是否创建成功:" + code + ":" + data.message);
					if(code == "1") {
						console.log("创建话题成功");
					} else {
						alert("创建专辑失败");
					}
				}
			});
	});
});

function removejscssfile(filename, filetype) {
	var targetelement = (filetype == "js") ? "script" : (filetype == "css") ? "link" : "none"
	var targetattr = (filetype == "js") ? "src" : (filetype == "css") ? "href" : "none"
	var allsuspects = document.getElementsByTagName(targetelement)
	for(var i = allsuspects.length; i >= 0; i--) {
		if(allsuspects[i] && allsuspects[i].getAttribute(targetattr) != null && allsuspects[i].getAttribute(targetattr).indexOf(filename) != -1)
			allsuspects[i].parentNode.removeChild(allsuspects[i])
	}
}
